package com.scvtc.service.research.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scvtc.domain.ResResult;
import com.scvtc.domain.dto.research.book.ResearchCourseBookDto;
import com.scvtc.domain.entity.research.ResearchCourseBook;
import com.scvtc.domain.vo.PageVo;
import com.scvtc.domain.vo.research.book.ResearchCourseBookVo;
import com.scvtc.mapper.research.ResearchCourseBookMapper;
import com.scvtc.service.research.ResearchCourseBookService;
import com.scvtc.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 科研教材信息(ResearchCourseBook)表服务实现类
 *
 * @author yiFei
 * @since 2022-06-02 16:17:08
 */
@Service("researchCourseBookService")
public class ResearchCourseBookServiceImpl extends ServiceImpl<ResearchCourseBookMapper, ResearchCourseBook> implements ResearchCourseBookService {
    /**
     * 查询科研教材信息
     *
     * @param current
     * @param size
     * @param researchCourseBookVo
     * @return
     */
    @Override
    public ResResult<PageVo<List<ResearchCourseBookVo>>> getAllResearchCourseBook(Long current, Long size, ResearchCourseBookVo researchCourseBookVo) {
        // 分页查询
        Page<ResearchCourseBook> page = getBaseMapper().getAllResearchCourseBook(new Page<ResearchCourseBook>(current, size), researchCourseBookVo);
        // BeanCopy
        List<ResearchCourseBookVo> researchCourseBookVos = BeanCopyUtils.copyBean(page.getRecords(), ResearchCourseBookVo.class);

        return ResResult.noMsgSuccess(new PageVo<>(researchCourseBookVos, page.getTotal()));
    }

    /**
     * 添加科研教材信息
     *
     * @param researchCourseBookDto
     * @return
     */
    @Override
    public ResResult<?> addResearchCourseBook(ResearchCourseBookDto researchCourseBookDto) {
        // 如果考虑后端安全性需要判断教工号是否存在 (代码可参考学生获奖表)
        ResearchCourseBook researchCourseBook = BeanCopyUtils.copyBean(researchCourseBookDto, ResearchCourseBook.class);
        if (save(researchCourseBook)) {
            return ResResult.success(researchCourseBook.getId());
        }
        return ResResult.fail();
    }

    /**
     * 修改科研教材信息
     *
     * @param researchCourseBookDto
     * @return
     */
    @Override
    public ResResult<?> updateResearchCourseBook(ResearchCourseBookDto researchCourseBookDto) {
        // 如果考虑后端安全性需要判断教工号是否存在 (代码可参考学生获奖表)
        ResearchCourseBook researchCourseBook = BeanCopyUtils.copyBean(researchCourseBookDto, ResearchCourseBook.class).setId(null);
        if (updateById(researchCourseBook)) {
            return ResResult.success();
        }
        return ResResult.fail();
    }

    /**
     * 删除单个科研教材信息
     *
     * @param id
     * @return
     */
    @Override
    public ResResult<?> deleteResearchCourseBook(Long id) {
        // 判断是否成功
        if (removeById(id)) {
            return ResResult.success();
        }
        return ResResult.fail();
    }

    /**
     * 删除多个科研教材信息
     *
     * @param ids
     * @return
     */
    @Override
    public ResResult<?> deleteMultipleResearchCourseBook(Long[] ids) {
        // 判断是否成功
        if (removeByIds(Arrays.asList(ids))) {
            return ResResult.success();
        }
        return ResResult.fail();
    }
}

